package com.ruoyi.crm.sale.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.crm.sale.domain.CrmClue;
import com.ruoyi.crm.sale.domain.CrmCustomer;
import com.ruoyi.crm.sale.mapper.CrmClueMapper;
import com.ruoyi.crm.sale.service.ICrmClueService;
import com.ruoyi.crm.sale.service.ICrmCustomerService;

/**
 * 线索管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-02
 */
@Service
public class CrmClueServiceImpl implements ICrmClueService 
{
    @Autowired
    private CrmClueMapper crmClueMapper;
    @Autowired
    private ICrmCustomerService crmCustomerService;

    /**
     * 查询线索管理
     * 
     * @param id 线索管理ID
     * @return 线索管理
     */
    @Override
    public CrmClue selectCrmClueById(Long id)
    {
        return crmClueMapper.selectCrmClueById(id);
    }

    /**
     * 线索转客户
     * 
     * @param id 线索管理ID
     * @return 线索管理
     */
    @Override
    public int clueToCustomer(Long id)
    {
    	CrmClue clue = crmClueMapper.selectCrmClueById(id);
    	CrmCustomer customer = new CrmCustomer();
    	customer.setCustomerName(clue.getClueName());
    	customer.setCustomerSource("线索转化-" + clue.getClueSource());
    	customer.setCustomerMsg(clue.getRemark());
    	customer.setCustomerLevel(clue.getClueLevel());
    	customer.setTrade(clue.getTrade());
    	customer.setCustomerPerson(clue.getClueContacts());
    	customer.setTel(clue.getTel());
    	customer.setMail(clue.getMail());
    	customer.setLeader(clue.getClueLeader());
    	customer.setLastTime(clue.getLastTime());
    	customer.setLastCon(clue.getLastCon());
    	customer.setAddress(clue.getAddress());
    	customer.setCreateTime(DateUtils.getNowDate());
    	customer.setUpdateTime(DateUtils.getNowDate());
    	customer.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
    	
    	clue.setIsCustomer(1);//已转客户
    	updateCrmClue(clue);
    	
        return crmCustomerService.insertCrmCustomer(customer);
    }

    /**
     * 查询线索管理列表
     * 
     * @param crmClue 线索管理
     * @return 线索管理
     */
    @Override
    @DataScope(deptAlias = "t", userAlias = "b")
    public List<CrmClue> selectCrmClueList(CrmClue crmClue)
    {
        return crmClueMapper.selectCrmClueList(crmClue);
    }

    /**
     * 新增线索管理
     * 
     * @param crmClue 线索管理
     * @return 结果
     */
    @Override
    public int insertCrmClue(CrmClue crmClue)
    {
        crmClue.setCreateTime(DateUtils.getNowDate());
        crmClue.setUpdateTime(DateUtils.getNowDate());
        crmClue.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        return crmClueMapper.insertCrmClue(crmClue);
    }

    /**
     * 修改线索管理
     * 
     * @param crmClue 线索管理
     * @return 结果
     */
    @Override
    public int updateCrmClue(CrmClue crmClue)
    {
        crmClue.setUpdateTime(DateUtils.getNowDate());
        crmClue.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        return crmClueMapper.updateCrmClue(crmClue);
    }

    /**
     * 批量删除线索管理
     * 
     * @param ids 需要删除的线索管理ID
     * @return 结果
     */
    @Override
    public int deleteCrmClueByIds(Long[] ids)
    {
        return crmClueMapper.deleteCrmClueByIds(ids);
    }

    /**
     * 删除线索管理信息
     * 
     * @param id 线索管理ID
     * @return 结果
     */
    @Override
    public int deleteCrmClueById(Long id)
    {
        return crmClueMapper.deleteCrmClueById(id);
    }
}
